/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package econutri.persistencia.dao;

import econutri.persistencia.entidades.Refeicao;
import econutri.persistencia.TransactionDAO;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author Leonardo Oliveira Moreira
 */
public class RefeicaoDAO {

    public List<Refeicao> getRefeicoes() {
        List<Refeicao> result = new ArrayList<Refeicao>();
        TransactionDAO dao = new TransactionDAO();
        String sql = "select " +
                "a.codigo, a.descricao " +
                "from refeicao a";
        ResultSet rs = dao.executeQuery(sql);
        try {
            while (rs != null && rs.next()) {
                Refeicao refeicao = new Refeicao();
                refeicao.setCodigo(rs.getLong("codigo"));
                refeicao.setDescricao(rs.getString("descricao"));
                result.add(refeicao);
            }
        } catch (SQLException ex) {
            Logger.getLogger(RefeicaoDAO.class.getName()).log(Level.SEVERE, null, ex);
            dao.rollback();
        }
        dao.commit();
        dao.close();
        return result;
    }

    public List<Refeicao> getRefeicoes(Long codigo, String descricao) {
        List<Refeicao> result = new ArrayList<Refeicao>();
        TransactionDAO dao = new TransactionDAO();
        String sql = "select " +
                "a.codigo, a.descricao " +
                "from refeicao a " +
                "where " +
                "(-1 = " + codigo + " or a.codigo = " + codigo + ") and " +
                "a.descricao like '" + descricao + "'";
        ResultSet rs = dao.executeQuery(sql);
        try {
            while (rs != null && rs.next()) {
                Refeicao refeicao = new Refeicao();
                refeicao.setCodigo(rs.getLong("codigo"));
                refeicao.setDescricao(rs.getString("descricao"));
                result.add(refeicao);
            }
        } catch (SQLException ex) {
            Logger.getLogger(RefeicaoDAO.class.getName()).log(Level.SEVERE, null, ex);
            dao.rollback();
        }
        dao.commit();
        dao.close();
        return result;
    }

    public Refeicao getRefeicao(Long codigo) {
        if (codigo == null) {
            return null;
        }
        Refeicao refeicao = new Refeicao();
        TransactionDAO dao = new TransactionDAO();
        String sql = "select " +
                "codigo, descricao from refeicao where codigo = " + codigo;
        ResultSet rs = dao.executeQuery(sql);
        try {
            while (rs != null && rs.next()) {
                refeicao.setCodigo(rs.getLong("codigo"));
                refeicao.setDescricao(rs.getString("descricao"));
            }
        } catch (SQLException ex) {
            Logger.getLogger(RefeicaoDAO.class.getName()).log(Level.SEVERE, null, ex);
            dao.rollback();
        }
        dao.commit();
        dao.close();
        return refeicao;
    }

    public boolean inserir(Refeicao refeicao) {
        if (refeicao == null) {
            return false;
        }
        boolean result = false;
        String sql = "insert into refeicao (descricao) values ('" + refeicao.getDescricao() + "')";
        TransactionDAO dao = new TransactionDAO();
        result = dao.insert(sql);
        if (result)
            dao.commit();
        else
            dao.rollback();
        dao.close();
        return result;
    }

    public boolean atualizar(Refeicao refeicao) {
        if (refeicao == null) {
            return false;
        }
        int sqlUpdate = 0;
        boolean result = false;
        String sql = "update refeicao set " +
                "descricao = '" + refeicao.getDescricao() +  "' " +
                "where codigo = " + refeicao.getCodigo() + "";
        TransactionDAO dao = new TransactionDAO();
        sqlUpdate = dao.update(sql);
        result = (sqlUpdate > 0);
        if (result) {
            dao.commit();
        }
        else
            dao.rollback();
        dao.close();
        return result;
    }


}
